#include<stdio.h>
#include<stdlib.h>
#include <string.h>

struct Student
{
	int num;//学号
	char name[20];//名字
	char sex[2];
	int age;
	struct Student *next;
};
void insert(struct Student **head);  //插入
void printall(struct Student *head);   //遍历所有链表
void dele(struct Student **head);   //删除 指定内容
void modify(struct Student **head); // 修改内容
void find(struct Student *head);    //查找学生信息
int modify_menu();
int main()
{
	struct Student *head = NULL;
	int x;
	do
	{
		printf("------------------------------------------\n");
		printf("             学生管理系统                 \n");	
		printf("                                          \n");
		printf("    1 增加学生          2 删除学生        \n");
		printf("                                          \n");
		printf("    3 修改资料          4 查找学生        \n");	
		printf("                                          \n");	
		printf("    5 显示所有学生      0 退出系统        \n");
		printf("                                          \n");
		printf("------------------------------------------\n");
		printf("请输入你需要使用的功能\n");
		scanf("%d",&x);
		switch(x)
		{
			case 0 : break;
			case 1 :
				insert(&head);
				break;
			case 2 :
				dele(&head);
				break;
			case 3 :
				modify(&head);
				break;
			case 4 :
				find(head);
				break;
			case 5 :
				printall(head);
				break;
			default :
					printf ("选择错误！！！\n");
					break;
		}
	}while(x);
}
void insert(struct Student **head)
{
	
	struct Student *p = (struct Student*)malloc(sizeof(struct Student));
	struct Student *stu=NULL;
	printf("num:");
	scanf("%d",&p->num);
	printf("name:");
	scanf("%s",(p->name));
	printf("sex:");
	scanf("%s",p->sex);
	printf("age:");
	scanf("%d",&p->age);
	p->next=NULL;
	if(*head == NULL)
    {
        *head = p;
    }
    else
    {
        stu = *head;
        while(stu->next != NULL)
        {
            stu = stu->next;
        }
        stu->next = p;
    }
    //system("pause");
    system("clear");
}
void printall(struct Student *head)
{
	printf("学号       名字       性别   年龄 \n");
	while(head != NULL) 
	{
	printf("%5d  %10s      %s       %d\n",head->num,head->name,head->sex,head->age);
	head=head->next;
	}
	//system("pause");
    //system("clear");
}
void dele(struct Student **head)
{
	char arr1[20];
	struct Student *p1 = NULL;//指向要删除的前一个结点
	struct Student *p2 = *head;//指向要删除的结点
	printf("请输入要删除的学生\n");
	scanf("%s",arr1);
	while(p2 != NULL)
	{
		if(p1==NULL&&strcmp(arr1,p2->name)==0)
		{
			*head = p2->next;
			free(p2);
			break ;
		}
		else if(strcmp(arr1,p2->name)==0)
		{
			p1->next = p2->next;
			free(p2);
			break ; 
		}
		p1=p2;
		p2=p2->next;
	}
	printall(*head);
	////system("pause");
    system("clear");
}
void modify(struct Student **head)  //修改 
{
	char arr[20];
	int x = 0;
	struct Student *p = *head;
	printf("请输入需要修改资料的名字\n");
	scanf("%s",arr);
	while(p!=NULL)
	{
		if(strcmp(arr,p->name) ==0)
		{
			printf("请选择修改的内容\n");
			x = modify_menu();
			printf("请输入新的内容\n");
			switch(x)
			{
			case 1 :
				scanf("%d",&p->num);
				break;
			case 2 :
				scanf("%s",p->name);
				break;
			case 3 :
				scanf("%s",p->sex);
				break;
			case 4:
				scanf("%d",&p->age);
				break;
			default :
					printf ("选择错误！！！\n");
					break;
			}
		printall(*head);
		break ;
		}	
	}
	//system("pause");
    system("clear");
}
int modify_menu()    //修改的菜单 
{
    int choose = 0;
    
    printf ("-----------------------------------\n");
    printf ("*    1  学号        2  姓名       *\n");
    printf ("*    3  性别        4  年龄       *\n");
    printf ("*	  0  取消修改                  *\n");
    printf ("-----------------------------------\n");
    scanf ("%d", &choose);
    return choose;
}
void find(struct Student *head)
{
	char arr[20];
	printf("请输入学生姓名\n");
	scanf("%s",arr);
	while(head!=NULL)
	{
		if(strcmp(arr,head->name)==0)
		{
			printf("学号       名字       性别   年龄 \n");
			printf("%5d  %10s      %s       %d\n",head->num,head->name,head->sex,head->age);
		}
	head=head->next;
	}
	//system("pause");
    system("clear");
}
